package com.acme.bankapp.service.bank.utils;

import com.acme.bankapp.domain.bank.account.Account;
import com.acme.bankapp.domain.bank.account.CheckingAccount;
import com.acme.bankapp.domain.bank.account.SavingAccount;
import com.acme.bankapp.domain.bank.client.Client;
import com.acme.bankapp.domain.bank.exception.DataIntegrityException;
import com.acme.bankapp.service.bank.utils.ClientInfoParser;

/**
 * This is factory for {@Client} objects
 * @author Alexander Kotov (kotov.alex.22@gmail.com)
 */
public class ClientFactory {
    public static Client createClient(final ClientInfoParser parser) {

        Account account;

        switch (parser.getAccountType()) {
            case CHECKING:
                account = new CheckingAccount(parser.getBalance(), parser.getOverdraft());
                break;
            case SAVING:
                account = new SavingAccount(parser.getBalance());
                break;
            default:
                throw new IllegalArgumentException("Unknown AccountType");
        }

        Client client = new Client(parser.getClientName(), parser.getGender(), parser.getEmailAddress(), account);
        try {
            account.connectWithClient(client);
        }
        catch (DataIntegrityException e) {
            System.out.println(e.getMessage());
        }
        return client;
    }
}
